package com.group.hugedatabehind.controller;

import com.group.hugedatabehind.mapper.LoginMapper;
import com.group.hugedatabehind.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
public class RegisterController {
    @Autowired
    private LoginMapper loginMapper;

    private static final Logger logger = LoggerFactory.getLogger(RegisterController.class);
    @PostMapping("/register")
    //这里只能用Integer，int是用在具体的某个对象中，比如int a = 1;
    public Integer register(@RequestBody User userfront) {
        String username = userfront.getUsername();
        String password = userfront.getPassword();
        if (StringUtils.isBlank(username)|| StringUtils.isBlank(password)){
            logger.error("注册失败，用户名和密码为空!");
            return 0;
        }
        else if (loginMapper.getUsername(username)!= null){
            logger.error("注册失败，用户名已存在!");
            return 1;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        loginMapper.userInsert(user);
        logger.info("用户{}注册成功！", username);
        return 2;
    }



}
